package com.example.kleinanzeigengenerator;

import com.example.kleinanzeigengenerator.R;
import com.example.kleinanzeigengenerator.common.Constants;
import com.example.kleinanzeigengenerator.listview.EbayListActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	private String sucheNach;
	private EditText suche;

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.erste_seite);
        
        Button startButton = (Button)findViewById(R.id.start);
        startButton.setOnClickListener(startListener);
        
        Button anzeigeButton = (Button)findViewById(R.id.ebay_durchsuchen);
        anzeigeButton.setOnClickListener(anzeigeListener);
        
        suche = (EditText)findViewById(R.id.suche_nach);
        
    }
    
    private OnClickListener startListener = new OnClickListener() {
    	
    	public void onClick(View v) {
    		Intent intent = new Intent(MainActivity.this, SiteAuswahl.class);
    		startActivity(intent);
    		finish();
    		
    	}
    };
    
    private OnClickListener anzeigeListener = new OnClickListener() {
    	
    	public void onClick(View v) {
    		Intent intent = new Intent(MainActivity.this, EbayListActivity.class);
    		sucheNach = suche.getText().toString();
    		intent.putExtra(Constants.SEARCH_TERM_TAG, sucheNach);
    		startActivity(intent);
    	}
    };
    


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
		try{
			menu.add(0,Constants.MENU_QUIT_TAG,1,"Ausgang");
			return(true);
		}catch(Exception x){
			Log.e("Menue","onCreateOptionsMenu",x);
			return(false);
		}
    }
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		try{
			switch(item.getItemId()){
			case Constants.MENU_QUIT_TAG:{this.finish();return(true);}
			default:{return(false);}
			}
		}catch(Exception x){
			Log.e("Menue","onOptionsItemSelected",x);
			return(false);
		}
	}
}
